Skip to content

fix(mcp): guard parameterized annotations in _call_handler and honor LIT_SERVER_API_KEY - #810

Open
mlubbad wants to merge 1 commit into
Lightning-AI:mainfrom
mlubbad:fix/mcp-pydantic-type-guard-and-api-key
Open

mlubbad wants to merge 1 commit into
Lightning-AI:mainfrom
mlubbad:fix/mcp-pydantic-type-guard-and-api-key

Conversation

@mlubbad

@mlubbad mlubbad commented Nov 27, 2026

Copy link
Copy Markdown

What does this PR do?

As a LitServe user exposing endpoints over MCP, two issues affect production deployments:

  1. Handlers annotated with parameterized generics (such as Optional[MyModel], Union[...], dict[str, Any], or untyped parameters where p.annotation is inspect._empty) raise TypeError: issubclass() arg 1 must be a class inside _call_handler.
  2. When LIT_SERVER_API_KEY is configured to protect the server, FastAPI routes enforce X-Api-Key, whereas the ASGI-mounted _MCPRequestHandler accepted unauthenticated HTTP requests.

Root Cause & Fix

  • Introduce _is_pydantic_model_class(annotation) wrapping isinstance(annotation, type) and issubclass(annotation, BaseModel) with a TypeError guard, and avoid re-wrapping values that are already BaseModel instances.
  • Check LIT_SERVER_API_KEY in _MCPRequestHandler.__call__ against the x-api-key header using secrets.compare_digest, returning HTTP 401 when missing or invalid.

Verification

  • Verified clean compilation (python3 -m py_compile src/litserve/mcp.py) and ruff check src/litserve/mcp.py.
Before submitting
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure to update the docs? (N/A, bug fix matching existing LIT_SERVER_API_KEY behavior)
  • Verified syntax and linter checks pass

…LIT_SERVER_API_KEY

- Avoid TypeError in issubclass() when handler parameter annotations are GenericAlias, Union, Optional, or inspect._empty, and skip re-instantiating existing BaseModel values.
- Enforce LIT_SERVER_API_KEY via constant-time secrets.compare_digest on the ASGI-mounted MCP request handler when configured.

Signed-off-by: Dr. Mohammed Lubbad <engmlubbad@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant